/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

import { BaseCommand } from '@amcax/base';
import { previewer } from '@app-cad/module';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { wsApi } from '@app-cad/server';
import { FeaturedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useMemo, useState } from 'react';
import { NumberInputFormat } from '@app-cad/common/components/command-form/constants';

type FormData = {
  radius1: number;
  radius2: number;
  height: number;
  angle: number;
};

function Cone({ title, onClose, placement, props }: FeaturedCmdDialogParams) {
  const [formData, setFormData] = useState<FormData>({
    radius1: 4,
    radius2: 2,
    height: 10,
    angle: 360,
  });

  useAsyncEffect(async () => {
    if (props == null) {
      setFormData({
        radius1: 4,
        radius2: 2,
        height: 10,
        angle: 360,
      });
      return;
    }

    const { radius1, radius2, height, angle } = props.params;

    // await initEditDependency(profile);

    const datas = {
      radius1: radius1,
      radius2: radius2,
      height: height,
      angle: angle,
    };

    setFormData(datas);
  }, []);

  // useAsyncEffect(async () => {
  //   if (!isFormDataValid()) {
  //     previewer.clear();
  //     return;
  //   }
  //   await execute(true);
  // }, [formData]);

  const isFormDataValid = () => {
    const { radius1, radius2, height, angle } = formData;

    return (
      radius1 !== null &&
      radius2 !== null &&
      radius1 !== radius2 &&
      height !== null &&
      angle !== null
    );
  };
  const finishValid = useMemo(isFormDataValid, [formData]);

  const execute = async (preview: boolean = false) => {
    const { radius1, radius2, height, angle } = formData;

    const params = {
      radius1: radius1,
      radius2: radius2,
      height: height,
      angle: angle,
    };
    const res =
      props == null
        ? await wsApi.createCone(radius1, radius2, height, angle)
        : await wsApi.setParams(props.shape, params, preview);

    if (preview) {
      await previewInCommand(res);
    }

    return res;
  };

  const checkAndFinish = async () => {
    if (!isFormDataValid()) {
      return;
    }

    execute();

    exit();
  };

  const exit = () => {
    previewer.clear();
    onClose();
  };

  // =================

  const schema: RJSFSchema = {
    properties: {
      radius1: {
        type: 'number',
        title: '半径1',
        default: 4,
        format: NumberInputFormat.Length,
      },
      radius2: {
        type: 'number',
        title: '半径2',
        default: 2,
        format: NumberInputFormat.Length,
      },
      height: {
        type: 'number',
        title: '高度',
        default: 10,
        format: NumberInputFormat.Length,
      },
      angle: {
        type: 'number',
        title: '旋转角度',
        default: 360,
        format: NumberInputFormat.Angle,
      },
    },
  };

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { initEditDependency, previewInCommand } = useCommand('cmd-cone', {
    onClose,
    finishDraw: checkAndFinish,
  });

  return (
    <PickableCommandForm
      isFormDataValid={finishValid}
      disabled={false}
      onClose={exit}
      title={title}
      placement={placement}
      schema={schema}
      formData={formData}
      onChange={(v) => {
        setFormData(v);
      }}
      extraUiSchema={{
        radius1: {
          'ui:placeholder': '请输入半径1',
          'ui:autofocus': true,
        },
        radius2: {
          'ui:placeholder': '请输入半径2',
        },
        height: {
          'ui:placeholder': '请输入高度',
        },
        angle: {
          'ui:placeholder': '请输入旋转角度',
        },
      }}
      submitText='完成'
      onSubmit={checkAndFinish}
    />
  );
}

export const cmdCone: BaseCommand = {
  id: 'CmdCone',
  label: 'command.cone',
  icon: 'commands/feature/cone',
  create: (label, onClose, props) => (
    <Cone
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
